unit ec_scrolltext;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  StdCtrls, Spin, ec_base;

type

  { TECScrollTextFrame }

  TECScrollTextFrame = class(TECBaseFrame)
    ForbidFastForwardCheckBox: TCheckBox;
    TextSizePanel: TPanel;
    TextPanel: TPanel;
    SpeedLabel: TLabel;
    SettingsPanel: TPanel;
    SpeedSpinEdit: TSpinEdit;
    TextLabel: TLabel;
    TextMemo: TMemo;
  private
    procedure InitialSetUp;
    procedure SetMemoFont;
    procedure UpdateLinePosition;
  public
    procedure InitNew; override;
    procedure InitExisting; override;
    procedure MakeResultingCommand; override;
    procedure UpdateLanguage(Code: String); override;
  end;

const
  NUM_CHARS = 55;

var
  ECScrollTextFrame: TECScrollTextFrame;

implementation

{$R *.lfm}

uses
  constants, fpjson, languagehelper, gamefont;

{ TECScrollTextFrame }

procedure TECScrollTextFrame.InitialSetUp;
begin
  SetMemoFont;
  UpdateLinePosition
end;

procedure TECScrollTextFrame.SetMemoFont;
begin
  with TextMemo.Font do begin
    BeginUpdate;
    Name := GetMonospacedFontName;
    Size := GetMonospacedFontSize;
    EndUpdate;
  end;
end;

procedure TECScrollTextFrame.UpdateLinePosition;
var
  X: Integer;
begin
  X := FindXPosOverMemo(TextMemo, NUM_CHARS);
  if akRight in TextSizePanel.Anchors then //RTL
    TextSizePanel.Left := TextMemo.Left + TextMemo.Width - X
  else
    TextSizePanel.Left := TextMemo.Left + X;
end;

procedure TECScrollTextFrame.InitNew;
begin
  InitialSetUp;

  TextMemo.Text := '';
  SpeedSpinEdit.Value := 2;
  ForbidFastForwardCheckBox.Checked := False;
end;

procedure TECScrollTextFrame.InitExisting;
  procedure InitSettings;
  var
    Params: TJSONArray;
  begin
    Params := GetFirstParams;

    if (Params.Count >= 1) and (Params[0].JSONType = jtNumber) then
      SpeedSpinEdit.Value := Params.Integers[0];

    if (Params.Count >= 2) and (Params[1].JSONType = jtBoolean) then
      ForbidFastForwardCheckBox.Checked := Params.Booleans[1];
  end;

  procedure InitText;
  var
    List: TStringList;
    I: Integer;
    Cmd: TJSONObject = nil;
    Params: TJSONArray = nil;
  begin
    List := TStringList.Create;

    I := ListIndex + 1;
    while I < EcList.Count do begin
      Cmd := EcList.Objects[I];

      if Cmd = nil then
         Break;
      if Cmd.Integers['code'] <> SCROLLING_TEXT_LINE_EC_CODE then
         Break;

      if Cmd.Find('parameters', Params) and (Params.Count >= 1) and
           (Params[0].JSONType = jtString) then
        List.Add(Params.Strings[0])
      else
        List.Add('');

      Inc(I);
    end;

    TextMemo.Text := List.Text;

    List.Free;
  end;

begin
  InitialSetUp;

  InitSettings;
  InitText;
end;

procedure TECScrollTextFrame.MakeResultingCommand;

  function CreateStartingCommand: TJSONObject;
  begin
    CreateStartingCommand := TJSONObject.Create([
      'code', SCROLLING_TEXT_START_EC_CODE,
      'indent', Indent,
      'parameters', TJSONArray.Create([
                      SpeedSpinEdit.Value,
                      ForbidFastForwardCheckBox.Checked
                    ])
    ]);
  end;

var
  List: TStringList;
  I: Integer;
begin
  ResultingCommand := TJSONArray.Create([CreateStartingCommand]);

  List := TStringList.Create();
  List.Text := TextMemo.Text;
  for I := 0 to List.Count -1 do begin
    ResultingCommand.Add(TJSONObject.Create([
      'code', SCROLLING_TEXT_LINE_EC_CODE,
      'indent', Indent,
      'parameters', TJSONArray.Create([List[I]])
    ]));
  end;
  List.Free;
end;

procedure TECScrollTextFrame.UpdateLanguage(Code: String);
begin
  inherited UpdateLanguage(Code);

  if IsLanguageRtl(Code) then begin
    TextSizePanel.Anchors := [akTop, akBottom, akRight];
    BiDiMode := bdRightToLeft;
  end else begin
    TextSizePanel.Anchors := [akTop, akBottom, akLeft];
    BiDiMode := bdLeftToRight;
  end;
end;

end.

